import pygame
import sys


def main():
    pygame.init()
    winx = 300
    winy = 300
    screen = pygame.display.set_mode((winx, winy))
    ball_surface = pygame.image.load('PYG02-ball.gif')
    print(f"ball_surface type: {ball_surface}")
    ball_rect = ball_surface.get_rect()
    ballx = 50
    bally = 20
    speedx = 10
    speedy = 18
    ball_rect[0] = ballx
    ball_rect[1] = bally
    w = ball_rect[2]
    h = ball_rect[3]
    print(f'ball_rect: {ball_rect[2]}')
    fclock = pygame.time.Clock()

    while True:
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill((31, 31, 31))
        if ball_rect[0] + w > winx or ball_rect[0] < 0:
            speedx *= -1
        if ball_rect[1] + h > winy or ball_rect[1] < 0:
            speedy *= -1
        ball_rect = ball_rect.move(speedx, speedy)
        screen.blit(ball_surface, ball_rect)
        pygame.display.update()
        fclock.tick(25)


if __name__ == '__main__':
    main()
